import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimediaWidgets import *


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('登录界面')
        self.setGeometry(700, 300, 500, 300)

        # 创建账号密码标签
        self.login_lable = QLabel("账号", self)
        self.pwd_lable = QLabel("密码", self)

        # 创建用户名和密码输入框
        self.username_line_edit = QLineEdit(self)
        self.password_line_edit = QLineEdit(self)
        self.password_line_edit.setEchoMode(QLineEdit.Password)
        self.username_line_edit.setPlaceholderText("请输入账号")
        self.password_line_edit.setPlaceholderText("请输入密码")

        # 创建登录按钮
        self.login_button = QPushButton('登录', self)
        self.login_button.resize(self.login_button.sizeHint())  # 设置默认大小
        self.login_button.clicked.connect(self.on_login_clicked)

        # 创建垂直布局并将控件添加到布局中
        layout = QVBoxLayout()
        layout1 = QHBoxLayout()
        layout2 = QHBoxLayout()

        # 保持账号标签与账号输入框高度一致
        layout1.addWidget(self.login_lable)
        layout1.addWidget(self.username_line_edit)

        # 保持密码标签与密码输入框高度一致
        layout2.addWidget(self.pwd_lable)
        layout2.addWidget(self.password_line_edit)

        layout.addLayout(layout1)
        layout.addLayout(layout2)
        layout.addWidget(self.login_button)

        # 设置中央部件为包含布局的 QWidget
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def on_login_clicked(self):
        # 获取用户名和密码
        username = self.username_line_edit.text()
        password = self.password_line_edit.text()

        # 在这里添加你的验证逻辑
        if username == "1" and password == "1":
            # 登录成功，创建新窗口并关闭当前窗口
            self.second_window = SecondWindow()
            self.second_window.show()
            self.close()
        else:
            # 登录失败，显示错误消息
            QMessageBox.critical(self, '错误', '用户名或密码错误')


class SecondWindow(QMainWindow):
    start_signal = pyqtSignal(str, int)
    stop_signal = pyqtSignal(str, int)
    null_signal = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.initUI()
        # 自定义工作信号

    def initUI(self):
        center_container = QWidget()

        # container = QStackedLayout(center_container)
        layout = QVBoxLayout(center_container)
        layout1 = QHBoxLayout()
        layout2 = QVBoxLayout()

        # 创建标签放图象
        self.pic_lable = QLabel(self)
        pixmap = QPixmap(r"C:\pythonProject1\gra_design\data\test_img\1.png")
        scaled_pixmap = pixmap.scaled(200, 300, Qt.KeepAspectRatio)
        self.pic_lable.setPixmap(scaled_pixmap)

        # 创建工作显示区
        self.text_edit = QTextEdit(self)
        # 设置 QTextEdit 的内容
        long_text = """
               吉林好风光
               """
        self.text_edit.setPlainText(long_text)
        self.text_edit.setReadOnly(True)
        self.text_edit.resize(200, 100)

        # 创建工作按钮、暂停按钮
        self.start_btn = QPushButton("开始工作", self)
        self.stop_btn = QPushButton("暂停工作", self)

        # 为按钮的点击绑定槽
        self.start_btn.clicked.connect(self.start_emit)
        self.stop_btn.clicked.connect(self.stop_emit)

        # 为接受按钮{信号连接槽
        self.start_signal.connect(self.start_mylot)
        self.stop_signal.connect(self.stop_mylot)

        # 设置布局结构
        layout1.addWidget(self.pic_lable)
        layout1.addWidget(self.text_edit)
        layout2.addWidget(self.start_btn)
        layout2.addWidget(self.stop_btn)

        layout.addLayout(layout1)
        layout.addLayout(layout2)

        self.setWindowTitle('操作界面')
        self.setCentralWidget(center_container)
        self.setGeometry(700, 400, 500, 300)

    def start_emit(self):
        '''
        发送开始工作信号
        :return:
        '''
        self.start_signal.emit('1', 888)

    def start_mylot(self, data, num):
        '''
        该槽接收开始工作信号
        :param data:
        :param num:
        :return:
        '''
        print(f"开始工作: {data}")
        print(f"开始指令: {num}")

    def stop_emit(self):
        '''
        发送停止工作信号
        :return:
        '''
        self.stop_signal.emit('0', 666)

    def stop_mylot(self, data, num):
        '''
        该槽接收停止工作信号
        :return:{
        '''
        print(f"暂停工作: {data}")
        print(f"暂停信号: {num}")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())
